package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import com.dingreading.cloud.soms.entity.CaClassrooms;
import com.dingreading.cloud.soms.service.CaClassService;
import com.dingreading.cloud.soms.service.CaClassroomsService;
import com.dingreading.cloud.soms.service.CaScheduleLessonService;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/front/caClassrooms")
@Api(tags = "门店-排课活动室管理")
public class FrontCaClassroomsController {

    @Resource
    private CaClassroomsService classroomsService;
    @Resource
    private CaScheduleLessonService scheduleLessonService;
    @Resource
    private CaClassService classService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "活动室名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<CaClassrooms> page = classroomsService.pageList(pageUtil, storeUid, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "根据门店uid获取活动室")
    @PostMapping(value = "/getClassrooms", produces = "application/json; charset=UTF-8")
    public R<Object> getClassrooms(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "活动室名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<CaClassrooms> list = classroomsService.getByStoreUid(storeUid, keyword, enabled);
        return R.ok(list);
    }

    @ApiOperation(value = "获取时间段详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        CaClassrooms data = classroomsService.getBean(storeUid, id);
        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "创建/编辑活动室")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "活动室名") @RequestParam(value = "name") String name,
            @ApiParam(value = "活动室容量") @RequestParam(value = "capacityNum") Integer capacityNum
    ) {
        if (StringUtils.isBlank(name))
            return R.fail("请填写正确的活动室名称");
        if (null == capacityNum || capacityNum <= 0)
            return R.fail("活动室容量必须大于0");

        boolean b = classroomsService.haveName(storeUid, id, name);
        if (b) return R.fail("活动室名称重复了");

        CaClassrooms data = null;
        if (null == id || id <= 0) {
            // 创建
            data = new CaClassrooms();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setUid(KitUtil.getUuid());
            data.setEnabled(1);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
        } else {
            // 编辑
            data = classroomsService.getById(id);
            if (null == data) return R.fail("活动室获取失败");
        }
        data.setRoomName(name);
        data.setCapacityNum(capacityNum);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        classroomsService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "删除活动室")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "活动室id") @RequestParam(value = "id") Long id
    ) {
        boolean b = classService.haveRoomId(storeUid, id);
        if (b) return R.fail("已关联社团，不能删除");
        // 删除前先判断，有没有排课逻辑
        b = scheduleLessonService.haveClassroom(storeUid, id, null);
        if (b) return R.fail("已关联活动记录，不能删除");

        long l = classroomsService.dropBean(storeUid, id);
        if (l <= 0) return R.failDel();
        return R.ok();
    }

    @ApiOperation(value = "更新活动室的状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled") Integer enabled
    ) {
        if (enabled.equals(0)) {
            // 删除前先判断，有没有排课逻辑
            boolean i = scheduleLessonService.haveClassroom(storeUid, id, Lists.newArrayList(0, 1));
            if (i) return R.fail("发现未开始/已开始的活动，不能禁用");
        }

        Date time = DateUtil.getNowDatetime();
        long l = classroomsService.updateEnabled(storeUid, id, enabled, staffUid, staffName, time);
        if (l <= 0) return R.fail("更新状态失败");
        return R.ok();
    }

}